Skip to content

MkShields

Show source on GitHub

Container for Shields.io / GitHub badges.

Example: Regular

Jinja

{{ ["version", "status", "codecov"] | MkShields }}

Python

MkShields(['version', 'status', 'codecov'])

PyPI Latest Version Package status Package status

[![PyPI Latest Version](https://img.shields.io/pypi/v/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Package status](https://img.shields.io/pypi/status/mknodes.svg)](https://pypi.org/project/mknodes/)
[![Package status](https://codecov.io/gh/phil65/mknodes/branch/main/graph/badge.svg)](https://codecov.io/gh/phil65/mknodes/)
<p><a href="https://pypi.org/project//"><img alt="PyPI Latest Version" src="https://img.shields.io/pypi/v/.svg"></a>
<a href="https://pypi.org/project//"><img alt="Package status" src="https://img.shields.io/pypi/status/.svg"></a>
<a href="https://codecov.io/gh///"><img alt="Package status" src="https://codecov.io/gh///branch/main/graph/badge.svg"></a></p>
MkShields
├── MkImage('https://img.shields.io/pypi/v/mknodes.svg', target='https://pypi.org/project/mknodes/', title='PyPI Latest Version')
├── MkImage('https://img.shields.io/pypi/status/mknodes.svg', target='https://pypi.org/project/mknodes/', title='Package status')
╰── MkImage('https://codecov.io/gh/phil65/mknodes/branch/main/graph/badge.svg', target='https://codecov.io/gh/phil65/mknodes/', title='Package status')

Example: Explicit

Jinja

{{ ["version", "status", "codecov"] | MkShields(user="mkdocs", project="mkdocs") }}

Python

MkShields(['version', 'status', 'codecov'], user='mkdocs', project='mkdocs')

PyPI Latest Version Package status Package status

[![PyPI Latest Version](https://img.shields.io/pypi/v/mkdocs.svg)](https://pypi.org/project/mkdocs/)
[![Package status](https://img.shields.io/pypi/status/mkdocs.svg)](https://pypi.org/project/mkdocs/)
[![Package status](https://codecov.io/gh/mkdocs/mkdocs/branch/main/graph/badge.svg)](https://codecov.io/gh/mkdocs/mkdocs/)
<p><a href="https://pypi.org/project/mkdocs/"><img alt="PyPI Latest Version" src="https://img.shields.io/pypi/v/mkdocs.svg"></a>
<a href="https://pypi.org/project/mkdocs/"><img alt="Package status" src="https://img.shields.io/pypi/status/mkdocs.svg"></a>
<a href="https://codecov.io/gh/mkdocs/mkdocs/"><img alt="Package status" src="https://codecov.io/gh/mkdocs/mkdocs/branch/main/graph/badge.svg"></a></p>
MkShields
├── MkImage('https://img.shields.io/pypi/v/mkdocs.svg', target='https://pypi.org/project/mkdocs/', title='PyPI Latest Version')
├── MkImage('https://img.shields.io/pypi/status/mkdocs.svg', target='https://pypi.org/project/mkdocs/', title='Package status')
╰── MkImage('https://codecov.io/gh/mkdocs/mkdocs/branch/main/graph/badge.svg', target='https://codecov.io/gh/mkdocs/mkdocs/', title='Package status')

Bases: MkContainer

__init__

__init__(
    shields: Sequence[BadgeTypeStr] | None = None,
    *,
    user: str | None = None,
    project: str | None = None,
    branch: str | None = None,
    **kwargs: Any
)

Parameters:

Name Type Description Default
shields Sequence[BadgeTypeStr] | None

Shields to include

None
user str | None

Github username for shields

None
project str | None

project name for shields

None
branch str | None

branch name for shields

None
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkContainer
mknodes.basenodes.mkcontainer
A node containing other MkNodes.
graph TD
  94721312307504["mkshields.MkShields"]
  94721311697232["mkcontainer.MkContainer"]
  94721308848336["mknode.MkNode"]
  94721311766592["node.Node"]
  140564252373184["builtins.object"]
  94721311697232 --> 94721312307504
  94721308848336 --> 94721311697232
  94721311766592 --> 94721308848336
  140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/templatenodes/mkshields/metadata.toml
[metadata]
name = "MkShields"
icon = "simple-icons:shieldsdotio"
group = "badge"
virtual_children = true

[examples.regular]
title = "Regular"
jinja = """
{{ ["version", "status", "codecov"] | MkShields }}
"""

[examples.explicit]
title = "Explicit"
jinja = """
{{ ["version", "status", "codecov"] | MkShields(user="mkdocs", project="mkdocs") }}
"""

[output.markdown]
template = """
{{ node.items | join("\n") }}
"""
# {% for s in node.shields %}
# {{ s.image_url.format(user=node.user, project=node.project, branch=node.branch) | MkImage(target=s.get_url(user=node.user, project=node.project, title=node.title)) }}
# {% endfor %}
mknodes.templatenodes.mkshields.MkShields
class MkShields(mkcontainer.MkContainer):
    """Container for Shields.io / GitHub badges."""

    ICON = "simple/shieldsdotio"
    VIRTUAL_CHILDREN = True
    ATTR_LIST_SEPARATOR = ""

    def __init__(
        self,
        shields: Sequence[badges.BadgeTypeStr] | None = None,
        *,
        user: str | None = None,
        project: str | None = None,
        branch: str | None = None,
        **kwargs: Any,
    ):
        """Constructor.

        Args:
            shields: Shields to include
            user: Github username for shields
            project: project name for shields
            branch: branch name for shields
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(**kwargs)
        self.block_separator = "\n"
        self._user = user
        self._project = project
        self._branch = branch
        self._shields = shields

    @property
    def user(self):
        match self._user:
            case None:
                return self.ctx.metadata.repository_username
            case str():
                return self._user

    @user.setter
    def user(self, value):
        self._user = value

    @property
    def project(self):
        if isinstance(self._project, str):
            return self._project
        return self.ctx.metadata.repository_name

    @project.setter
    def project(self, value):
        self._project = value

    @property
    def shields(self):
        shields = self._shields or [i.identifier for i in badges.SHIELDS]
        return [s for s in badges.SHIELDS if s.identifier in shields]

    @property
    def branch(self) -> str:
        if isinstance(self._branch, str):
            return self._branch
        return self.ctx.git.main_branch or "main"

    @property
    def items(self) -> list[mknode.MkNode]:
        return [
            mkimage.MkImage(
                s.get_image_url(user=self.user, project=self.project, branch=self.branch),
                target=s.get_url(user=self.user, project=self.project),
                title=s.title,
                parent=self,
            )
            for s in self.shields
        ]

    @items.setter
    def items(self, value):
        pass